package com.syb;

import com.zrt.dao.impl.ParseDaoImpl;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class test1 {
    public static void main(String[] args) throws IOException {
        ParseDaoImpl parseDao = new ParseDaoImpl();
        //pdf路径
        String s = parseDao.parseFile("D:\\a.pdf");
        FindPdfAuthors(s);
    }

    public static void  FindPdfAuthors(String AllContents) throws IOException {
        String Beginning = AllContents.substring(0, 1000);
        InputStream inputStreamTokenizer = new
                FileInputStream("OpenNLP_Models/en-token.bin");
        TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer);

        //Instantiating the TokenizerME class
        TokenizerME tokenizer = new TokenizerME(tokenModel);

        //Tokenizing the sentence in to a string array
        String sentence = Beginning;
        String tokens[] = tokenizer.tokenize(sentence);

        //Loading the NER-person model
        InputStream inputStreamNameFinder = new
                FileInputStream("OpenNLP_Models/en-ner-person.bin");
        TokenNameFinderModel model = new TokenNameFinderModel(inputStreamNameFinder);

        //Instantiating the NameFinderME class
        NameFinderME nameFinder = new NameFinderME(model);

        //Finding the names in the sentence
        Span nameSpans[] = nameFinder.find(tokens);

        //Printing the names and their spans in a sentence
        for(Span s: nameSpans)
            System.out.println(s.toString()+"  "+tokens[s.getStart()]);
    }
}

